﻿using Microsoft.Extensions.Logging;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Collections.Concurrent;

namespace XYS.Service.Core.Consumers.Base
{
    /// <summary>
    /// MQ 消费者
    /// </summary>
    public interface IConsumerBase
    {
        /// <summary>
        /// 队列名
        /// </summary>
        string QuqueName { get; }

        /// <summary>
        /// 是否自动 ACK
        /// </summary>
        bool AutoACK { get; }
        /// <summary>
        /// 并发数量
        /// </summary>
        int ParallelCount { get; }
        /// <summary>
        /// 基础消费者
        /// </summary>
        public List<AsyncEventingBasicConsumer> AsyncEventingBasicConsumers { get; set; }
        /// <summary>
        /// 待Ack的tag 集合
        /// </summary>
        List<ConcurrentDictionary<ulong, string>> Tags { get; }
        /// <summary>
        /// 消费
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="event"></param>
        /// <returns></returns>
        Task Consumer_Received(object sender, BasicDeliverEventArgs @event);

        /// <summary>
        /// BasicNack
        /// </summary>
        /// <param name="index"></param>
        /// <param name="deliveryTag"></param>
        /// <param name="message"></param>
        void BasicNack(int index ,ulong deliveryTag, string message = "");

        /// <summary>
        /// BasicNack
        /// </summary>
        /// <param name="index"></param>
        /// <param name="deliveryTags"></param>
        /// <param name="message"></param>
        void BasicNack(int index, List<ulong> deliveryTags, string message = "");

        /// <summary>
        /// ACK
        /// </summary>
        /// <param name="index"></param>
        /// <param name="deliveryTag"></param>
        void BasicAck(int index, ulong deliveryTag);

        /// <summary>
        /// ACK
        /// </summary>
        /// <param name="index"></param>
        /// <param name="deliveryTags"></param>
        void BasicAck(int index, List<ulong> deliveryTags);

        /// <summary>
        /// 关闭通道
        /// </summary>
        /// <param name="count">关闭通道的数量</param>
        void CloseChannel(int count);


        /// <summary>
        /// 创建通道
        /// </summary>
        void CreateChannel(int count);

    }

    /// <summary>
    /// 初始化消费者配置
    /// </summary>
    public class ConsumerSetting
    {
        /// <summary>
        /// 连接通道
        /// </summary>
        public IModel Model { get; set; }

        /// <summary>
        /// 队列名称
        /// </summary>
        public string QueueName { get; set; }
    }
}
